package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.base.dto.LoginAuthDto;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdSpecInfoDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.cmc.entity.CmsSecKillSku;
import com.yuke.cloud.service.cmc.service.CmsSecKillSkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by wg on 2019/05/28.
*/
@Api(value = "WEB - CmsSecKillSkuController", tags = "处理CmsSecKillSku相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/sec_kill_sku")
public class CmsSecKillSkuController extends BaseController {
    @Resource
    private CmsSecKillSkuService cmsSecKillSkuService;
    @Autowired
    private PmcFeignApi pmcFeignApi;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsSecKillSku", value = "该数据记录的POJO") @RequestBody CmsSecKillSku cmsSecKillSku) {
        cmsSecKillSku.setCreateId(getLoginAuthDto().getUserId());
        cmsSecKillSku.setCreateTime(new Date());
        cmsSecKillSkuService.save(cmsSecKillSku);
        return WrapMapper.ok(cmsSecKillSku.getSecKillSkuId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsSecKillSkuService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsSecKillSku", value = "需要修改的内容") @RequestBody CmsSecKillSku cmsSecKillSku) {

        if (cmsSecKillSku == null || cmsSecKillSku.getSecKillSkuId() == null) {
            return WrapMapper.error("秒杀商品记录ID不能为空");
        }

        cmsSecKillSku.setUpdateId(getLoginAuthDto().getUserId());
        cmsSecKillSku.setUpdateTime(new Date());

        cmsSecKillSkuService.update(cmsSecKillSku);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsSecKillSku> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsSecKillSku cmsSecKillSku = cmsSecKillSkuService.findById(id);

        // 获取商品信息
        ProdSpecInfoDto prodSpecInfoDto = pmcFeignApi.getSpecInfoDtoBySpecId(cmsSecKillSku.getSpecId());
        if (prodSpecInfoDto != null) {
            cmsSecKillSku.setSpecName(prodSpecInfoDto.getSpecName());
            cmsSecKillSku.setCategoryName(prodSpecInfoDto.getCategoryName());
            cmsSecKillSku.setMarketPrice(prodSpecInfoDto.getSalePrice());
        }

        return WrapMapper.ok(cmsSecKillSku);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsSecKillSku>> list(@ApiParam(name = "id", value = "对应的秒杀活动ID") @RequestParam(required = false) Long secKillId,
                                                 @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<CmsSecKillSku> list = null;
        if (secKillId != null) {
            //PageHelper.orderBy("sort");
            CmsSecKillSku cmsSecKillSku = new CmsSecKillSku();
            cmsSecKillSku.setSecKillId(secKillId);
            list =  cmsSecKillSkuService.select(cmsSecKillSku);
        } else {
            list = cmsSecKillSkuService.findAll();
        }
        System.out.println(list);
        // 获取商品信息
        String specIds = "";
        int i = 0;
        for (CmsSecKillSku cmsSecKillSku : list) {
            ++i;
            specIds += String.valueOf(cmsSecKillSku.getSpecId());
            if (i < list.size()) {
                specIds += ",";
            }
        }
        List<ProdSpecInfoDto> specInfoDtosBySpecIds = pmcFeignApi.getSpecInfoDtosBySpecIds(specIds);
        if (specInfoDtosBySpecIds != null) {
            for (CmsSecKillSku cmsSecKillSku : list) {
                for (ProdSpecInfoDto prodSpecInfoDto : specInfoDtosBySpecIds) {
                    if (cmsSecKillSku.getSpecId().equals(prodSpecInfoDto.getSpecId()) ) {
                        cmsSecKillSku.setSpecName(prodSpecInfoDto.getSpecName());
                        cmsSecKillSku.setCategoryName(prodSpecInfoDto.getCategoryName());
                        cmsSecKillSku.setMarketPrice(prodSpecInfoDto.getSalePrice());
                        break;
                    }
                }
            }
        }
        System.out.println(list);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @DeleteMapping("/ids")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    public Wrapper deleteByIds(@ApiParam(name = "ids", value = "要删除的秒杀活动ID集") @RequestBody String secKillSkuIds,
                               @ApiParam(name = "secKillId", value = "商品对应的秒杀活动ID") @RequestParam Long secKillId) {
        if (secKillId == null) {
            return WrapMapper.error("秒杀活动ID不能为空");
        }

        cmsSecKillSkuService.deleteByIdsExt(secKillId, secKillSkuIds);
        return WrapMapper.ok();
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据秒杀活动ID及商品ID检查唯一性")
    public Wrapper ifExist(@ApiParam(name = "cmsAppTopic", value = "记录topicName") @RequestBody CmsSecKillSku cmsSecKillSku) {

        if (cmsSecKillSku.getSecKillId() == null || cmsSecKillSku.getSpecId() == null) {
            return WrapMapper.ok(false);
        }

        int count = cmsSecKillSkuService.selectCount(cmsSecKillSku);

        if (cmsSecKillSku.getSecKillSkuId() != null) {  // 排除自身
            count = 0;
        }
        return WrapMapper.ok(count > 0);
    }

    @PutMapping("/batchUpdate")
    @ApiOperation(httpMethod = "PUT", value = "批量修改")
    public Wrapper batchUpdate(@RequestBody List<CmsSecKillSku> cmsSecKillSkus) {
        for (CmsSecKillSku cmsSecKillSku : cmsSecKillSkus) {
            LoginAuthDto loginAuthDto = getLoginAuthDto();
            cmsSecKillSku.setUpdateId(loginAuthDto.getUserId());
            cmsSecKillSku.setUpdateTime(new Date());
            cmsSecKillSkuService.update(cmsSecKillSku);
        }
        return WrapMapper.ok();
    }
}
